﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Tools;
using ICSharpCode.SharpZipLib.Zip;

namespace ShowScreen
{
    public partial class FormResult : Form
    {
        public FormResult(ScanData sd)
        {
            InitializeComponent();
            _ActiveScandata = sd;
            _BE = new IDbeTool();
        }
        private void FormResult_Load(object sender, EventArgs e)
        {
            _ActiveExamResult = new ExamResult(_ActiveScandata);
            InitExamResultData();
            InitImage();
            ShowItem();
        }
        private void InitExamResultData()
        {
            _ExistImglistMode = false;
            if (_ActiveExamResult.ImgList.Count > 0)
            {
                _ExistImglistMode = true;
                listBox1.Items.AddRange(_ActiveExamResult.ImgList.Select( r=> r.Replace(_ActiveScandata.Fullpath,"")).ToArray());
            }
        }
        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }        
        private void InitImage()
        {
            _BE.B = 0;
            _BE.E = _ActiveExamResult.ImgList.Count;
            _BE.MoveToTop();
        }
        private void buttonPre_Click(object sender, EventArgs e)
        {
            if(_ExistImglistMode)
            PreviousShow();
        }
        private void buttonNext_Click(object sender, EventArgs e)
        {
            if (_ExistImglistMode)
            NextShow();
        }
        private void NextShow()
        {
            _BE.MoveNext();
            if (_BE.HasNext)
            {
                ShowItem();
            }
            else
            {
                _BE.MovePrevious();
                textBoxMsg.Text = (_BE.ActiveIndex+1) + "/" + _BE.E  +   " 已经是最后一组,没有可以显示的图片了";
            }           
        }
        private void PreviousShow()
        {
            _BE.MovePrevious();
            if (_BE.HasNext)
            {
                ShowItem();
            }
            else
            {
                _BE.MoveNext();
                textBoxMsg.Text = (_BE.ActiveIndex+1) + "/" + _BE.E  +  "  已经是第一组,没有可以显示的图片了";
            }           
        }
        private void ShowItem()
        {
            try
            {
                string imgfilename = _ActiveExamResult.ImgList[_BE.ActiveIndex];
                pictureBox1.Image = GetNetImage(imgfilename);
                textBoxMsg.Text = (_BE.ActiveIndex + 1) + "/" + _BE.E;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        private Image GetNetImage(string imgfilename) //需要详细
        {
            string localfilename = imgfilename;
            if (!File.Exists(localfilename))
                return null;
            return Bitmap.FromFile(localfilename);
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
                return;
            _ExistImglistMode = true;
            if (_ExistImglistMode)
            {
                if (_BE.MoveTo(listBox1.SelectedIndex))
                    ShowItem();
                else
                    MessageBox.Show("没有该项目");
            }
        }
        private void buttonShowTotalResult_Click(object sender, EventArgs e)
        {
            if (_ActiveExamResult == null)
                return;
            int index1 = _ActiveExamResult.ImgList.FindIndex( r => r.Contains("小题成绩单") );
            if(index1 <0 )
                index1 = _ActiveExamResult.ImgList.FindIndex(r => r.Contains("总分"));
            if (index1 < 0)
            {
                MessageBox.Show("找不到总成绩单");
                return;
            }
            if (_BE.MoveTo(index1))
            {
                ShowItem();
            }
            else
            {
                MessageBox.Show("找不到总成绩单");
            }
        }

        public ScanData _ActiveScandata;
        private IDbeTool _BE;
        private ExamResult _ActiveExamResult;
        private bool _ExistImglistMode;
    }
}
